package com.gopay.scheduler.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.cps.CpsGenReceiptOrder;
import com.gopay.common.exception.CPSException;
import com.gopay.common.exception.GopayException;
import com.gopay.scheduler.dao.ReceiptOrderDAO;

/**
 * 收款类子订单表更新
 * @ReceiptOrderDAOImpl.java
 * @author dongdh
 * @2016年5月25日 下午6:30:50  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("receiptOrderDAO")
public class ReceiptOrderDAOImpl extends BaseDAOImpl<CpsGenReceiptOrder, Serializable>  implements ReceiptOrderDAO {

    /**
     * 更新鉴权结果
     * @throws GopayException 
     * @throws CPSException 
     */
    public boolean updateAuthResult(String gopayOrderId, String result) throws GopayException {
        
        CpsGenReceiptOrder payOrder = find(CpsGenReceiptOrder.class, gopayOrderId);
        if (null == payOrder) {
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E5041);
        }
        
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("gopayOrderId", gopayOrderId);
        params.put("authenticationResult", result);
        
        StringBuilder bud = new StringBuilder(128);
        bud.append("UPDATE CpsGenReceiptOrder pay SET"
                + " pay.authenticationResult = :authenticationResult , lastUpdTs = sysdate, remark1 = 'cron跑批补扣' ");
        
        bud.append(" WHERE");
        bud.append(" pay.gopayOrderId = :gopayOrderId");
        return updateByHql(bud.toString(), params) > 0;
    }

    @Override
    public boolean cleanDelaySplitOrder(String gopayOrderId)throws GopayException{
        Map<String,Object> map = new HashMap<>();
        map.put("gopayOrderId",gopayOrderId);
        String sql = "update cps_gen_receipt_order aa" +
                "   set aa.other_recv_status = '3'" +
                " where aa.gopay_order_id = :gopayOrderId";
        return  updateBySql(sql,map) > 0;
    }
}
